<template>
  <div class="month-card">
    <div class="content-2">
      <div class="left">
        <a-button type="primary" @click="addRules">增加停车计费规则</a-button>
      </div>
    </div>
    <a-table
      :pagination="pagination"
      @change="tableChange"
      :dataSource="dataSource"
      :columns="columns"
    >
      <template #bodyCell="{ text, column, index }">
        <div v-if="column.dataIndex === 'index'">{{ index + 1 }}</div>
        <template v-if="column.key === 'chargeCeiling'">
          {{ text || '无' }}
        </template>
        <template v-if="column.key === 'chargeType'">
          {{ text === 'duration' ? '时长收费' : text === 'turn' ? '按次收费' : '分段收费' }}
        </template>
        <template v-if="column.key === 'actions'">
          <a-button type="link">编辑</a-button>
          <a-button type="link">删除</a-button>
        </template>
      </template>
    </a-table>
    <!-- 弹出框组件 -->
    <AddRules ref="modelRef" @ADD_SUCCESS="search" />
  </div>
</template>

<script setup lang="ts">
import { ref, reactive, computed } from 'vue';
import { getRuleListReqeust } from '@/apis/cart.ts';
import AddRules from './AddRules.vue';

const modelRef = ref<any>(null);
const formState = reactive<any>({
  page: 1,
  pageSize: 10,
});

const dataSource = ref([]);
const columns = ref([
  {
    title: '序号',
    dataIndex: 'index',
    key: 'index',
  },
  {
    title: '计费规则编号',
    dataIndex: 'ruleNumber',
    key: 'ruleNumber',
  },
  {
    title: '计费规则名称',
    dataIndex: 'ruleName',
    key: 'ruleName',
  },
  {
    title: '免费时长（分钟）',
    dataIndex: 'freeDuration',
    key: 'freeDuration',
  },
  {
    title: '收费上限（元）',
    dataIndex: 'chargeCeiling',
    key: 'chargeCeiling',
  },
  {
    title: '计费方式',
    dataIndex: 'chargeType',
    key: 'chargeType',
  },
  {
    title: '计费规则',
    dataIndex: 'ruleNameView',
    key: 'ruleNameView',
  },
  {
    title: '操作',
    // 操作这一列不需要dataIndex，因为dataIndex是去渲染dataSource这个数组里面的元素的键的值的
    // dataIndex: 'cardStatus',
    key: 'actions',
  },
]);

const total = ref(0);
// 表格自带分页，不需要额外去找分页组件,
// 不是我们单独的分页组件，而是依附于表格自带的插件，自动集成了分页组件，但是没有文档
// react + ant-design
// vue3 + ant-design-vue
const pagination = computed(() => ({
  position: ['bottomRight'],
  total: total.value,
  current: formState.page,
  pageSize: formState.pageSize,
  showQuickJumper: true, // 是否显示跳转的结构
  showSizeChanger: true, // 显示每页显示条数切换器，下拉框
  pageSizeOptions: ['2', '3', '5', '10'],
  showTotal: (total: number, range: any) => `${range[0]}-${range[1]}  共${total} 条`,
}));

// table内置的分页插件，触发事件change
const tableChange = (pagination: any) => {
  formState.page = pagination.current;
  formState.pageSize = pagination.pageSize;
  search();
};

const search = async () => {
  const res = await getRuleListReqeust({
    page: formState.page,
    pageSize: formState.pageSize,
  });

  dataSource.value = res.data.rows;
  total.value = res.data.total;
};

search();

// 添加规则
const addRules = () => {
  modelRef.value.open();
};
</script>

<style lang="scss">
.month-card {
  margin-top: 15px;
  padding: 15px;
  background-color: #fff;
  .ant-form {
    border-bottom: 1px solid #cccccc55;
    padding-bottom: 15px;
    .ant-form-item-control-input {
      width: 200px;
      .ant-input {
        border-radius: 5px;
      }
      .ant-select-selector {
        border-radius: 5px;
      }
    }
  }
  .content-2 {
    display: flex;
    justify-content: space-between;
    margin: 20px 0;
    .left {
      .ant-btn:first-child {
        margin-right: 10px;
      }
      .ant-btn {
        border-radius: 5px;
      }
    }
    .right {
      border: 1px solid rgb(145, 213, 255);
      background-color: rgba(145, 213, 255, 0.25);
      line-height: 32px;
      padding: 0 15px;
    }
  }
}
</style>
